package com.foeye.jaxrs2.server.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.alibaba.fastjson.JSON;
import com.foeye.ws.server.entity.User;

@Path("/user")
public class UserService {
	
	@POST
	 @Path("/add")
	@Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
	public Response saveUser(String user) {
		User u = new User();
		u.setId("1");
		u.setAge(14);
		u.setName("张三");
		System.out.println("==> add user ...");
		System.out.println("==> " + user);
		User u2 = JSON.parseObject(user, User.class);
		
		System.out.println("==> user = " + u2); 
		//2、这里解决返回Response为UTF-8
        //如果是网页，可以只保留上面的“1”中的@Produces，这里的header可不用；如果对Response编码，经测试该Header是必须的
        //Content-Type: application/json;charset=UTF-8
		return Response.ok(u)
				.header(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON + ";charset=UTF-8")
                .build();
	}
	
	@DELETE
    @Path("/{id}")
	public Response deleteBook(String id) {
		System.out.println("==> delete user ...");
		return Response.ok().build();
	}
	
	@GET
    @Produces(MediaType.APPLICATION_JSON)
	public Response getAll() {
		User user1 = new User();
		user1.setId("1");
		user1.setAge(14);
		user1.setName("张三");
		User user2 = new User();
		user2.setId("2");
		user2.setAge(22);
		user2.setName("李四");
		User user3 = new User();
		user3.setId("3");
		user3.setAge(23);
		user3.setName("王五");
		
		List<User> list = new ArrayList<>();
		list.add(user1);
		list.add(user2);
		list.add(user3);
		
		GenericEntity<List<User>> userWrapper = new GenericEntity<List<User>>(list) {};
		
		System.out.println("==> get all user ...");
		return Response.ok(userWrapper)
				.header(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON + ";charset=UTF-8")
				.build();
	}
	
	
	@GET
    @Path("/get/{id}")
	@Produces({ MediaType.APPLICATION_JSON, "text/html; charset=UTF-8" })
	public Response getById(@PathParam("id") String id) {
		System.out.println("执行  getById ...");
		System.out.println("-------->" + id);
		User user = new User();
		user.setId("1");
		user.setAge(14);
		user.setName("张三");
		System.out.println("==> get one user ...");
		return Response.ok(user)
				.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + ";charset=UTF-8")
				.build();
	}
	
	@GET
    @Path("/get")
	@Produces({ MediaType.APPLICATION_JSON, "text/html; charset=UTF-8" })
	public Response getUserById(@QueryParam("id") String id) {
		System.out.println("执行  getUserById ...");
		System.out.println("-------->" + id);
		User user = new User();
		user.setId("1");
		user.setAge(14);
		user.setName("张三");
		System.out.println("==> get one user ...");
		return Response.ok(user)
				.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + ";charset=UTF-8")
				.build();
	}
	
	
	

}
